﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HtmlExtractor.Processor;
using System.Xml.Linq;

namespace HtmlExtractor.Config
{
    public class GroupInfo
    {
        public string Name
        { get; set; }
        public string OutputPath;
        public RegexConfig ValidExpression;
        /// <summary>
        /// 只有独立组可以执行split
        /// </summary>
        public RegexConfig SplitExpression;
        public IEnumerable<ReplaceExpression> ReplaceExpressions;
        public string PostProcessorName;
        public CaptureInfo Capture;
        public IPageItemConfig SubRule
        {
            get;
            set;
        }
        /// <summary>
        /// 全部结果
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        internal PageResult GetParsResult(SegmentMatchGroup group, ParserContext context)
        {
            PageResult t=GetSubResult(group,context);
            PageResult pr = t==null?new PageResult():t;
            if (!String.IsNullOrEmpty(OutputPath))
            {
               
                SimpleItem s = GetGroupResult(group, context);
                if (SplitExpression != null)
                {
                   List<string> sc=SegmentRegex.Split(s.Value, SplitExpression.Expression, SplitExpression.Options, true);

                   foreach (string ss in sc)
                   {
                       pr.Add(new SimpleItem(OutputPath, ss));
                   }
                }
                else
                {
                    pr.Add(GetGroupResult(group, context));
                }
               
            }
            return pr;
        }
        /// <summary>
        /// 不包含subRule的结果集，组合组用到
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        internal SimpleItem GetGroupResult(SegmentMatchGroup group,ParserContext context)
        {
            string postValue = group.Value;
            if (!string.IsNullOrEmpty(PostProcessorName))
            {
                postValue = ProcessorFactory.CreateProcessor(PostProcessorName).GetResult(postValue,context);
            }
            if (ValidExpression != null)
            {
                //todo valid
            }
            if (ReplaceExpressions != null)
            {
                foreach (ReplaceExpression re in ReplaceExpressions)
                {
                   postValue= re.GetResult(postValue,context);
                }
            }
           
            SimpleItem g = new SimpleItem();
            g.OutputPath = OutputPath;
            g.Value = postValue;
            return g;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="group"></param>
        /// <returns></returns>
        internal PageResult GetSubResult(SegmentMatchGroup group,ParserContext context)
        {
            if (SubRule != null)
            {
                return SubRule.GetParseResult(group.Value,context);
            }
            return null ;
        }
        public GroupInfo()
        { }
        public GroupInfo(XElement groupInfoNode)
        {
           
            Name = (string)groupInfoNode.Attribute("name");
            PostProcessorName = (string)groupInfoNode.Element("PostProcessor");
            OutputPath = (string)groupInfoNode.Attribute("outputPath");
            ValidExpression = groupInfoNode.Element("ValidExpression") == null ? null : new RegexConfig(groupInfoNode.Element("ValidExpression"));

            SplitExpression = groupInfoNode.Element("SplitExpression") == null ? null : new RegexConfig(groupInfoNode.Element("SplitExpression"));

            ReplaceExpressions = from r in groupInfoNode.Elements("ReplaceExpression") select ReplaceExpression.Create(r);
            if (groupInfoNode.Element("Capture") != null)
            {              
                Capture = new CaptureInfo(groupInfoNode.Element("Capture"));
            }
            //groupinf sub
            XElement groupinfoSub = groupInfoNode.Element("Sub");
            if (groupinfoSub != null)
            {
                IPageItemConfig subRule = RuleBuildFactory.Create(groupinfoSub.Elements().Single());
                SubRule = subRule;
            }
           
        }
      
    }
}
